package com.chatroom.controller;

import com.chatroom.dto.ChatRoomDTO;
import com.chatroom.dto.UserDTO;
import com.chatroom.model.ChatRoom;
import com.chatroom.model.User;
import com.chatroom.service.ChatRoomService;
import com.chatroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/chatrooms")
public class ChatRoomController {

    private final ChatRoomService chatRoomService;
    private final UserService userService;

    @Autowired
    public ChatRoomController(ChatRoomService chatRoomService, UserService userService) {
        this.chatRoomService = chatRoomService;
        this.userService = userService;
    }

    @PostMapping
    public ResponseEntity<?> createChatRoom(@RequestBody Map<String, String> request) {
        String name = request.get("name");
        String creatorUsername = request.get("creatorUsername");
        
        Optional<User> creatorOpt = userService.findByUsername(creatorUsername);
        
        if (creatorOpt.isPresent()) {
            ChatRoom chatRoom = chatRoomService.createChatRoom(name, creatorOpt.get());
            return ResponseEntity.status(HttpStatus.CREATED).body(new ChatRoomDTO(chatRoom));
        } else {
            Map<String, String> response = new HashMap<>();
            response.put("error", "创建者不存在");
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/search")
    public ResponseEntity<?> findByRoomCode(@RequestParam String roomCode) {
        Optional<ChatRoom> chatRoomOpt = chatRoomService.findByRoomCode(roomCode);
        
        if (chatRoomOpt.isPresent()) {
            return ResponseEntity.ok(new ChatRoomDTO(chatRoomOpt.get()));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/{roomCode}/join")
    public ResponseEntity<?> joinChatRoom(@PathVariable String roomCode, @RequestBody Map<String, String> request) {
        String username = request.get("username");
        
        Optional<ChatRoom> chatRoomOpt = chatRoomService.findByRoomCode(roomCode);
        Optional<User> userOpt = userService.findByUsername(username);
        
        if (chatRoomOpt.isPresent() && userOpt.isPresent()) {
            chatRoomService.addUserToChatRoom(chatRoomOpt.get(), userOpt.get());
            return ResponseEntity.ok().build();
        } else {
            Map<String, String> response = new HashMap<>();
            response.put("error", "聊天室或用户不存在");
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/{roomCode}/members")
    public ResponseEntity<?> getChatRoomMembers(@PathVariable String roomCode) {
        Optional<ChatRoom> chatRoomOpt = chatRoomService.findByRoomCode(roomCode);
        
        if (chatRoomOpt.isPresent()) {
            List<UserDTO> members = chatRoomService.getChatRoomMembers(chatRoomOpt.get()).stream()
                    .map(UserDTO::new)
                    .collect(Collectors.toList());
            return ResponseEntity.ok(members);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
} 